Page({
  data: {
    contentArr: [],
    isLoading: false
    // selectedTab: '已完成'
  },
  // showPaid: function() {
  //   this.setData({
  //     selectedTab: '已完成'
  //   });
  //   console.log("用户点击了已支付选项卡");
  // },
  // showUnpaid: function() {
  //   this.setData({
  //     selectedTab: '未完成'
  //   });
  //   console.log("用户点击了未支付选项卡");
  // },

  onLoad(options) {
    this.setData({ isLoading: true });
    let id = 0;
    wx.getStorage({
      key: 'vipuser',
      success: (res) => {
        id = res.data.memberId;
        console.log(res.data);
        console.log(id);
        wx.request({
          url: `http://127.0.0.1:9090/CourseFavorite/selectByMemberId/${id}`,
          method: 'GET',
          success: (res) => {
            this.setData({
              contentArr: res.data.data
            });
            this.formatfavoriteTime();
            const contentArrs = this.data.contentArr.map(item => {
              return new Promise((resolve) => {
                this.getImage(item, resolve);
              });
            });
            Promise.all(contentArrs).then(() => {
              this.setData({ isLoading: false });
            }).catch((error) => {
              wx.showToast({
                title: '部分图片加载失败，请稍后重试',
                icon: 'none'
              });
              console.error('图片加载错误:', error);
              this.setData({ isLoading: false });
            });
          },
          fail: (err) => {
            wx.showToast({
              title: '数据加载失败，请稍后重试',
              icon: 'none'
            });
            console.error('请求数据失败:', err);
            this.setData({ isLoading: false });
          }
        });
      },
      fail: (err) => {
        wx.showToast({
          title: '获取存储失败，请检查权限或网络',
          icon: 'none'
        });
        console.error('获取存储失败:', err);
        this.setData({ isLoading: false });
      }
    });
  },

  formatfavoriteTime() {
    let newContentArr = this.data.contentArr.map(item => {
      let newItem = {...item };
      if (newItem.favoriteTime) {
        newItem.favoriteTime = this.formatTime(newItem.favoriteTime);
      }
      return newItem;
    });
    this.setData({
      contentArr: newContentArr
    });
    console.log(this.data.contentArr);
  },

  formatTime(time) {
    const date = new Date(time);
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    return `${year}-${month}-${day}`;
  },

  getImage: function(item, callback) {
    const that = this;
    wx.request({
      url: 'http://localhost:9090/file/download2',
      method: 'GET',
      data: {
        fileName: item.courses.courseImage,
      },
      success: (res) => {
        if (res.data) {
          item.courses.courseImage = 'data:image/png;base64,' + res.data;
        }
        let index = that.data.contentArr.findIndex(c => c === item);
        let newContentArr = [...that.data.contentArr];
        newContentArr[index] = item;
        that.setData({
          contentArr: newContentArr
        });
        callback();
      },
      fail: (err) => {
        wx.showToast({
          title: '图片加载失败',
          icon: 'none'
        });
        callback();
      }
    });
  },

  onReady() {},

  onShow() {},

  onHide() {},

  onUnload() {},

  onPullDownRefresh() {},

  onReachBottom() {},

  onShareAppMessage() {}
});